require "common/Define"
require "framework/BaseController"
require "FairyGUI"

PerspectiveMain = BaseController:New({mControllerName = "PerspectiveMain"});

function PerspectiveMain.Main()
	-- body
	PerspectiveMain:Awake();
end

function PerspectiveMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	UIPackage.AddPackage("UI/Perspective");

	Camera.main.transform.localPosition = Vector3(250, 1.8, 300);

	self:CreatePanel("Perspective", "Panel", "UIPanel1");
	self:CreatePanel("Perspective", "Panel2", "UIPanel2");

	-- 
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function PerspectiveMain:CreatePanel(packageName, componentName, gName)
	-- body
	local go = GameObject();
	go.name = gName;
	go.layer = UnityEngine.LayerMask.NameToLayer("Default");
	if(gName == "UIPanel1") then
		go.transform.localPosition = Vector3(250, 4.8, 314);
		go.transform.localScale = Vector3(0.014, 0.014, 0.014);
		go.transform.localEulerAngles = Vector3(0, 40, 0);
	else
		go.transform.localPosition = Vector3(240, 9, 313);
		go.transform.localScale = Vector3(0.016, 0.016, 0.016);
		go.transform.localEulerAngles = Vector3(0, -40, 0);
	end

	local panel = go:AddComponent(typeof(UIPanel));
	panel.packageName = packageName;
	panel.componentName = componentName;
	-- 设置renderMode的方式
	panel.container.renderMode = UnityEngine.RenderMode.WorldSpace;
	-- 设置fairyBatching
	panel.container.fairyBatching = true;
	-- 设置sortingOrder的方式
	-- panel.SetSortingOrder(1, true);
	-- 设置hitTestMode的方式
	panel:SetHitTestMode(FairyGUI.HitTestMode.Default);
	panel:CreateUI();
end


function PerspectiveMain:InitView()
	local g2 = GameObject.Find("UIPanel2"):GetComponent(typeof(UIPanel)).ui;
	self._list = g2:GetChild("mailList").asList;
	self._list:SetVirtual();
	self._list.itemRenderer = self.RenderItem;
	self._list.numItems = 20;
end

function PerspectiveMain.RenderItem(index, obj)
	obj.text = index .. " Mail title here";
end

function PerspectiveMain:OnKeyDown(context)
	if (context.inputEvent.keyCode == KeyCode.Escape) then
		Application.Quit();
	end
end